package com.jse.security;

public class RSATest {
    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJk+CcF7SwbGrvX5Sw3ZGrsWIxck2hpsh7RwTVa/nz3Lyhvy0oNXncOni7RTunUj1dmm9TbcE7ICZ0eYPprrlVZiVdtoqpSobSxuXLsx/IULse66d8MdOthXUrYGyZSpT1urBhrJMP3M04AhWWaFhZhrr5mNbNNN4tXjH9JtImcwIDAQAB";
    private static String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAImT4JwXtLBsau9flLDdkauxYjFyTaGmyHtHBNVr+fPcvKG/LSg1edw6eLtFO6dSPV2ab1NtwTsgJnR5g+muuVVmJV22iqlKhtLG5cuzH8hQux7rp3wx062FdStgbJlKlPW6sGGskw/czTgCFZZoWFmGuvmY1s003i1eMf0m0iZzAgMBAAECgYAWmjvpiR9/aKB86TgVgqFfEspuEzhb2XnQfw91PMMY5Gf9LtptY7O8kcKqCarOR32bdjbZ/wsi+po2wowAhVsfQ0Ikn4DVZVPDaOYwaO4u3IifMUB+ErSZFWDk4wiYJCWVLGabIIMRTe9NiI2qUHDwwBXtb8FKj82NPc0hBCdAwQJBAPxBLbSBe1ihjvSkfEqXYgP7XvRNYBKoxGplYJ223xIH/O+6+WqKNuNdKHdNcYDI2AH6/XIrVLmJYEqiYOCIZpMCQQCLns+abnIDRbVELVq/LvtQHSBJh9L1CzKrmUK2yVa5F5qpoSGdl7laDOb1lJmk1ouNMr2yuBCHgIZVV2I60kyhAkEAhGY5dQqZERYLEIDc89GtEmX3msaZ7HFEVovLQDxhrPcgXTuH+dF9qayVtrihIHVKWw+/wxLXL2LZdZLwwikVnQJBAIcmmJFF3gl63cd8G767LLxVJV7oInXffBINjj2/0bnjtmwDNLYHG32mxZ7tVHjy0S1B88zfTejMqbxMlSKZVWECQQDP/HnI+o5oUeua0RR/ItHVxVuKFZJa98iLpDmGNkAdsXy7KvPERhRU0WOL0o80rANxZqbraew3hNTqlXFNFbA6";

    public static void setUp() throws Exception {
//        Map<String, Object> keyMap = RSACoder.initKey();
//        publicKey = RSACoder.getPublicKey(keyMap);
//        privateKey = RSACoder.getPrivateKey(keyMap);
        System.err.println("公钥: \n\r" + publicKey);
        System.err.println("私钥： \n\r" + privateKey);
    }

    public static void test() throws Exception {
        System.err.println("公钥加密——私钥解密");
        String inputStr = "abc";
        byte[] data = inputStr.getBytes();

        byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);

        byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData,
                privateKey);

        String outputStr = new String(decodedData);
        System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);

    }

    public static void testSign() throws Exception {
        System.err.println("私钥加密——公钥解密");
        String inputStr = "sign";
        byte[] data = inputStr.getBytes();

        byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey);

        byte[] decodedData = RSACoder
                .decryptByPublicKey(encodedData, publicKey);

        String outputStr = new String(decodedData);
        System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);

        System.err.println("私钥签名——公钥验证签名");
        // 产生签名
        String sign = RSACoder.sign(encodedData, privateKey);
        System.err.println("签名:\r" + sign);

        // 验证签名
        boolean status = RSACoder.verify(encodedData, publicKey, sign);
        System.err.println("状态:\r" + status);

    }

    public static void main(String[] args) {
        try {
            setUp();
            test();
            testSign();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}